<?php

namespace App\Http\Controllers;
use DB;

use App\Datatables;
use App\Game;
use App\Gamepic;
use App\GameCommodity;
use App\GamePrizeLog;
use App\GameStatus;
use Illuminate\Http\Request;

use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Core\OssUtil;
/**
 * Class GameController
 * @package App\Http\Controllers 
 */
class GameController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * 游戏列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function gameList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::GameList());
        }
 
        return view('game.gameList');
    }

    /**
     * 添加游戏
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function gamePublish(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Game::GamePublish($request));
        }

        return view('game.gamePublish');
    }

    /**
     * 游戏详情
     * @param Request $request
     * @param Game $game
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function gameInfo(Request $request, Game $game)
    {
        if ($request->isMethod('post')) {
            return response()->json(Game::GameModify($game, $request));

        }
        return view('game.gameInfo')->with(compact('game'));
    }

    /**
     * 游戏状态列表
     * @param Request $request
     * @param Game $game
     * @return \Illuminate\Http\JsonResponse
     */
    public function gameStatusList(Game $game)
    {
        return response()->json(Datatables::GameStatusList($game->id));
    }


    /**
     * 游戏奖励列表
     * @param Game $game
     * @return \Illuminate\Http\JsonResponse
     */
    public function gameCommodityList(Game $game)
    {
        return response()->json(Datatables::GameCommodityList($game->id));
    }

    /**
     * 游戏上下架
     * @param Game $game
     */
    public function gameDelflag(Game $game)
    {
        $game->del_flag = $game->del_flag === Game::DEL_NO ? Game::DEL_YES : Game::DEL_NO;
        return $game->save() ? ['status' => 1, 'message' => '修改成功'] : ['status' => 2, 'message' => '修改失败'];
    }

    /**
     * 游戏状态
     * @param Request $request
     * @param GameStatus $gameStatus
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function statusInfo(Request $request, GameStatus $gameStatus)
    {
        if ($request->isMethod('post')) {
            return response()->json(GameStatus::GameStatusModify($gameStatus, $request));
        }
        return view('game.statusInfo')->with(compact('gameStatus'));
    }

    /**
     * 添加游戏状态
     * @param Request $request
     * @param $game_id
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function statusPublish(Request $request, $game_id)
    {
        if ($request->isMethod('post')) {
            return response()->json(GameStatus::GameStatusPublish($game_id, $request));
        }
        return view('game.statusPublish')->with(compact('game_id'));
    }


    /**
     * 删除游戏状态
     * @param GameStatus $gameStatus
     * @return array
     * @throws \Exception
     */
    public function statusDel(GameStatus $gameStatus)
    {
        return $gameStatus->delete() ? ['status' => 1, 'message' => '删除成功'] : ['status' => 2, 'message' => '删除失败'];
    }

    /**
     * 游戏奖励详情
     * @param Request $request
     * @param GameCommodity $gameCommodity
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function commodityInfo(Request $request, GameCommodity $gameCommodity)
    {
        if ($request->isMethod('post')) {
            return response()->json(GameCommodity::GameCommodityModify($gameCommodity, $request));
        }

        return view('game.commodityInfo')->with(compact('gameCommodity'));
    }

    /**
     * 添加奖品
     * @param Request $request
     * @param $game_id
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function commodityPublish(Request $request, $game_id)
    {
        if ($request->isMethod('post')) {
            return response()->json(GameCommodity::GameCommodityPublish($game_id, $request));
        }
        return view('game.commodityPublish')->with(compact('game_id'));
    }

    /**
     * 删除奖品
     * @param GameCommodity $gameCommodity
     * @return array
     * @throws \Exception
     */
    public function commodityDel(GameCommodity $gameCommodity)
    {
        return $gameCommodity->delete() ? ['status' => 1, 'message' => '删除成功'] : ['status' => 2, 'message' => '删除失败'];
    }

    /**
     * 游戏奖品领取记录
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function prizeList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::GamePrizeList());
        }
        return view('game.prizeList');
    }

    /**
     * 收货信息
     * @param GamePrizeLog $gamePrizeLog
     * @return array
     */
    public function prizeInfo(GamePrizeLog $gamePrizeLog)
    {
        $address = $gamePrizeLog->sheng . $gamePrizeLog->shi . $gamePrizeLog->xian . $gamePrizeLog->detail;
        return [
            'name' => $gamePrizeLog->user_name,
            'phone' => $gamePrizeLog->phone,
            'address' => $address,
        ];
    }


    /**
     * 拒绝
     * @param Request $request
     * @param GamePrizeLog $gamePrizeLog
     * @return array
     */
    public function prizeRefuse(Request $request, GamePrizeLog $gamePrizeLog)
    {
        return GamePrizeLog::Refuse($gamePrizeLog, $request);
    }

    /**
     * 同意
     * @param Request $request
     * @param GamePrizeLog $gamePrizeLog
     * @return array
     */
    public function prizeAgree(Request $request, GamePrizeLog $gamePrizeLog)
    {
        return GamePrizeLog::Agree($gamePrizeLog, $request);
    }
		//游戏图片
	public function picList(Request $request){
		if($request->isMethod('post')) {
            return response()->json(Datatables::Gmpiclist());
        }
        return view('game.piclist');
	}
	// 添加游戏图片
	public function picPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = Gamepic::picPublish($request);
            if ($return['status'] === 1) {
                return redirect('/game/piclist')->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('game.gmpicPublish');
    }
	//修改游戏图片
	public function picModify(Request $request, Gamepic $Gamepic)
	{
        if ($request->isMethod('post')) {
            $return = Gamepic::picModify($request, $Gamepic);
			
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('game.gmpicModify')->with(compact('Gamepic'));		
	}
	//删除游戏图片
	public function picDel($pic_id)
	{
		$access_id = env('OSS_ACCESSID');
        $access_key = env('OSS_ACCESSKEY');
        $bucket = env('OSS_BUCKET');
        $endpoint = env('OSS_ENDPOINT'); // OSS 外网节点或自定义外部域名
		DB::beginTransaction();
        //删除
		$object = DB::table('gave_pic')->where('id', $pic_id)->value('path');
		$datas=explode("/",$object);
		try{
			$ossClient = new OssClient($access_id, $access_key, $endpoint);
			$ossClient->deleteObject($bucket, $datas[3]);
		} catch(OssException $e) {
			printf($e->getMessage() . "\n");
		    DB::rollBack();
			return ['status' => 2, 'message' => '删除失败'];
		}
        if (!DB::table('gave_pic')->where('id', $pic_id)->delete()) {
			 DB::rollBack();
            return ['status' => 2, 'message' => '删除失败'];
        }
		 DB::commit(); 
        return ['status' => 1, 'message' => '删除成功'];
	}
	
}
